package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.IUserDao;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserImpl implements IUserDao {
    @Override
    public int add(UserModel userModel) {
        String sql = "insert into user(username,password,phone,sex,birthday,hobby,introduce) " +
                "values('" + userModel.getUsername() + "'," +
                "'" + userModel.getPassword() + "'," +
                "'" + userModel.getPhone() + "'," +
                "'" + userModel.getSex() + "'," +
                "'" + userModel.getBirthday() + "'," +
                "'" + userModel.getHobby() + "'," +
                "'" + userModel.getIntroduce() + "')";
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public boolean findByUsername(String username) {
        String sql = "select * from user where username='" + username + "'";
        ResultSet resultSet = JDBC.getResultSet(sql);
        boolean isHave = false;
        try {
            while (resultSet.next()) {
                isHave = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isHave;
    }

    /**
     * 根据用户名和密码查询数据
     *
     * @param userModel
     * @return
     */
    @Override
    public boolean findByUsernameAndPassword(UserModel userModel) {
        String sql = "select * from user where `username`='" + userModel.getUsername() + "' and `password`='" + userModel.getPassword() + "'";
        ResultSet resultSet = JDBC.getResultSet(sql);
        boolean isHave = false;
//        UserModel model=null;
        try {
            while (resultSet.next()) {
                isHave = true;
//                model=new UserModel();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isHave;
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {
        StringBuilder sql = new StringBuilder("select * from user where 1=1 ");
        if (!"".equals(userModel.getUsername())) {
            sql.append("and  username like '%").append(userModel.getUsername()).append("%'");
        }
        if (!"".equals(userModel.getStartBirthday()) && !"".equals(userModel.getEndBirthday())) {
            sql.append(" and DATE_FORMAT(birthday,'%Y-%m-%d') between '").append(userModel.getStartBirthday()).append("' and '").append(userModel.getEndBirthday()).append("'");
        } else {
            if (!"".equals(userModel.getStartBirthday())) {
                sql.append(" and DATE_FORMAT(birthday,'%Y-%m-%d') >= ").append("DATE_FORMAT('").append(userModel.getStartBirthday()).append("','%Y-%m-%d')");
            }
            if (!"".equals(userModel.getEndBirthday())) {
                sql.append(" and DATE_FORMAT(birthday,'%Y-%m-%d') <= ").append("DATE_FORMAT('").append(userModel.getEndBirthday()).append("','%Y-%m-%d')");
            }
        }
        if (!"".equals(userModel.getIntroduce())) {
            sql.append(" and  introduce like '").append(userModel.getIntroduce()).append("'");
        }
        if (!"全部".equals(userModel.getSex())) {
            sql.append(" and sex='").append(userModel.getSex()).append("'");
        }
        if (!"".equals(userModel.getPhone())) {
            sql.append(" and phone like '%").append(userModel.getPhone()).append("'");
        }
        if (!"".equals(userModel.getHobby())) {
            String[] hb = userModel.getHobby().split(",");

            for (int i = 0; i < hb.length; i++) {
                if (i == 0) {
                    sql.append(" and FIND_IN_SET('").append(hb[i].trim()).append("',hobby)");
                } else {
                    sql.append(" or FIND_IN_SET('").append(hb[i].trim()).append("',hobby)");
                }

            }
        }

        sql.append(" limit ").append((userModel.getPageModel().getPageNum() + -1) * userModel.getPageModel().getPageSize()).append(",").append(userModel.getPageModel().getPageSize());

        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        List<UserModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                UserModel model = new UserModel();
                model.setId(resultSet.getInt("id"));
                model.setUsername(resultSet.getString("username"));
                model.setPhone(resultSet.getString("phone"));
                model.setSex(resultSet.getString("sex"));
                model.setBirthday(resultSet.getString("birthday"));
                model.setHobby(resultSet.getString("hobby"));
                model.setIntroduce(resultSet.getString("introduce"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int del(int id) {
        String sql = "delete from user where id=" + id;
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public UserModel findById(int id) {
        String sql = "select * from user where id=" + id;
        ResultSet resultSet = JDBC.getResultSet(sql);
        UserModel userModel = new UserModel();
        try {
            while (resultSet.next()) {
                userModel.setId(resultSet.getInt("id"));
                userModel.setUsername(resultSet.getString("username"));
                userModel.setPhone(resultSet.getString("phone"));
                userModel.setSex(resultSet.getString("sex"));
                userModel.setHobby(resultSet.getString("hobby"));
                userModel.setIntroduce(resultSet.getString("introduce"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userModel;
    }

    @Override
    public int edit(UserModel userModel) {
        String sql = "update user set " +
                "password='" + userModel.getPassword() + "' and " +
                "sex='" + userModel.getSex() + "' where id=" + userModel.getId();
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public int getCount(UserModel userModel) {
        StringBuilder sql = new StringBuilder("select count(*) count from user where 1=1 ");
        if (!"".equals(userModel.getUsername())) {
            sql.append("and  username like '%").append(userModel.getUsername()).append("%'");
        }
        if (!"".equals(userModel.getStartBirthday()) && !"".equals(userModel.getEndBirthday())) {
            sql.append(" and DATE_FORMAT(birthday,'%Y-%m-%d') between '").append(userModel.getStartBirthday()).append("' and '").append(userModel.getEndBirthday()).append("'");
        } else {
            if (!"".equals(userModel.getStartBirthday())) {
                sql.append(" and DATE_FORMAT(birthday,'%Y-%m-%d') >= ").append("DATE_FORMAT('").append(userModel.getStartBirthday()).append("','%Y-%m-%d')");
            }
            if (!"".equals(userModel.getEndBirthday())) {
                sql.append(" and DATE_FORMAT(birthday,'%Y-%m-%d') <= ").append("DATE_FORMAT('").append(userModel.getEndBirthday()).append("','%Y-%m-%d')");
            }
        }
        if (!"".equals(userModel.getIntroduce())) {
            sql.append(" and  introduce like '").append(userModel.getIntroduce()).append("'");
        }
        if (!"全部".equals(userModel.getSex())) {
            sql.append(" and sex='").append(userModel.getSex()).append("'");
        }
        if (!"".equals(userModel.getPhone())) {
            sql.append(" and phone like '%").append(userModel.getPhone()).append("'");
        }
        if (!"".equals(userModel.getHobby())) {
            String[] hb = userModel.getHobby().split(",");

            for (int i = 0; i < hb.length; i++) {
                if (i == 0) {
                    sql.append(" and FIND_IN_SET('").append(hb[i].trim()).append("',hobby)");
                } else {
                    sql.append(" or FIND_IN_SET('").append(hb[i].trim()).append("',hobby)");
                }

            }
        }

        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        int count=0;
        try {
            while (resultSet.next()) {
                count=resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}
